package com.hangwei.aicabinet.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.hangwei.aicabinet.R;
import com.hangwei.aicabinet.manager.NetManager;
import com.hangwei.aicabinet.manager.SerialPortProxy;
import com.hangwei.aicabinet.model.bean.OrderBean;
import com.hangwei.aicabinet.net.API;
import com.hangwei.aicabinet.net.ResponseInfo;
import com.hangwei.aicabinet.util.LoadingDialog;
import com.hangwei.aicabinet.util.NotFastClickListener;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;

import java.util.List;

import rx.Observable;

/**
 * author: 张龙臣
 * date: 2018-08-28.
 */

public class SubOrderAdapter extends RecyclerView.Adapter<SubOrderAdapter.MyViewHolder> {


    private List<OrderBean.EntryListBean> mData;
    private Context context;

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_sub_order, null);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (mData != null)
            holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        if (mData == null)
            return 0;
        else return mData.size();
    }

    public void setNewData(List<OrderBean.EntryListBean> newData) {
        mData = newData;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tvToolName;
        private TextView tvToolSpe;
        private TextView tvPlace;
        private ImageView ivOpen;
        private SimpleDraweeView sdvTool;
        private TextView tvStatus;
        private View viewDot;


        public MyViewHolder(View itemView) {
            super(itemView);
            tvToolName = itemView.findViewById(R.id.tvToolName);
            tvToolSpe = itemView.findViewById(R.id.tvToolSpe);
            tvPlace = itemView.findViewById(R.id.tvPlace);
            ivOpen = itemView.findViewById(R.id.ivOpen);
            sdvTool = itemView.findViewById(R.id.sdvTool);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            viewDot = itemView.findViewById(R.id.viewDot);
        }

        public void setData(final OrderBean.EntryListBean entryListBean) {
            sdvTool.setImageURI(API.BASE_URL_UNDER_LINE + entryListBean.toolPic);
            tvToolName.setText(entryListBean.insName);
            tvToolSpe.setText(entryListBean.insModel);
            tvPlace.setText(entryListBean.storeAddress);
            tvStatus.setText(entryListBean.entryStatusName);
            if ("待借用".equals(entryListBean.entryStatusName)) {
                tvStatus.setTextColor(context.getResources().getColor(R.color.wait));
                viewDot.setBackgroundResource(R.drawable.wait);
            } else if ("借用中".equals(entryListBean.entryStatusName)) {
                tvStatus.setTextColor(context.getResources().getColor(R.color.inuse));
                viewDot.setBackgroundResource(R.drawable.inuse);
            } else if ("已归还".equals(entryListBean.entryStatusName)) {
                tvStatus.setTextColor(context.getResources().getColor(R.color.back));
                viewDot.setBackgroundResource(R.drawable.back);
            }

            if (entryListBean.canOpen == 1) {
                ivOpen.setImageResource(R.drawable.open);
                ivOpen.setEnabled(true);
            } else {
                ivOpen.setImageResource(R.drawable.open_enable);

                ivOpen.setEnabled(false);
            }
            ivOpen.setOnClickListener(new NotFastClickListener() {
                @Override
                public void isNotFastClick(View view) {
//                    openLock("LOCK_001");
//                    entryListBean.storeAddress
                    String[] set = entryListBean.spaceNumSet;
                    if (set != null && set.length != 0) {
                        for (String num : set) {
                            String[] split = num.split("-");
                            int i = Integer.parseInt(entryListBean.storeAddress.substring(6, 7));
                            int j = Integer.parseInt(split[1]);
                            if (i == j) {
                                openLock(num);
                                break;
                            }
                        }
                    }
//                    openLock(entryListBean.spaceNumSet[0]);
//                    OnLockOpen(null);
                }

                @Override
                protected void doubleClick(View view) {
                    ToastUtil.showCenterToast("请勿连续点击开锁");
                }
            });
        }
    }

    private void openLock(final String lockNum) {
        String type = SPUtil.getString(SPKey.CABINET_TYPE);

        int which = SPUtil.getInt(SPKey.OPENED_DOOR);
        if (which != 0) {
            ToastUtil.showCenterToast("已有柜门打开，请先关闭");
            return;
        }

//        String[] split = lockNum.split("-");
//        int whicha = Integer.parseInt(split[1]);
//        switch (whicha) {
//            case 1:
//                if (!LockManager.which1) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which1 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 2:
//                if (!LockManager.which2) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which2 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 3:
//                if (!LockManager.which3) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which3 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 4:
//                if (!LockManager.which4) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which4 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 5:
//                if (!LockManager.which5) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which5 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 6:
//                if (!LockManager.which6) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which6 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 7:
//                if (!LockManager.which7) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which7 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//            case 8:
//                if (!LockManager.which8) {
//                    ToastUtil.showCenterToast("请勿在30秒内开启同一锁");
//                    SPUtil.setInt(SPKey.OPENED_DOOR, 0);
//                    return;
//                } else {
//                    LockManager.which8 = false;
//                    LockManager.limitTime(whicha);
//                }
//                break;
//
//        }

        Log.i("openLock--", "------which------" + lockNum);
        final Dialog dialog = LoadingDialog.showDialog(context, "柜门打开中");
        dialog.show();
        NetManager.getInstance().doSubscribe(new NetManager.Model<String>() {
            @Override
            protected Observable<ResponseInfo<String>> getObservable() {
                return NetManager.getResponseInfoAPI().updateLockStatus(lockNum, 1);
            }

            @Override
            protected void onNext(String data) {
                if (dialog.isShowing()) dialog.dismiss();
//                ToastUtil.showCenterToast("柜门已打开");
                SPUtil.setString(SPKey.OPENED_DOOR_SPACE_NUM, lockNum);
                String[] split = lockNum.split("-");
                try {
                    int which = Integer.parseInt(split[1]);
                    SPUtil.setInt(SPKey.OPENED_DOOR, which);
                    SerialPortProxy.openLock(which);
//                    SerialPortProxy.loopLockStatus();//不能在这里
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onError() {
                if (dialog.isShowing()) dialog.dismiss();
                ToastUtil.showCenterToast("柜门开启失败");
            }
        });
    }


}
